package sd.qd.xiaoming.coffeedesk.support;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import sd.qd.xiaoming.coffeedesk.service.InitDBService;

@Component
public class SystemInit implements InitializingBean {

	@Value(value = "${clearAndInit}")
	private boolean clearAndInit = false;
	@Autowired
	private InitDBService initDBService;

	public boolean isClearAndInit() {
		return this.clearAndInit;
	}

	/**
	 * 是否要清空数据库重新创建数据
	 * 
	 * @param clearAndInit
	 */
	public void setClearAndInit(boolean clearAndInit) {
		this.clearAndInit = clearAndInit;
	}


	/**
	 * 在Spring容器启动后，初始化系统数据库
	 */
	@Override
	@Transactional
	public void afterPropertiesSet() throws Exception {
		if (!isClearAndInit()) {
			return;
		}

		this.initDBService.initDatabase();

	}

}
